package com.huawei.fastengine.fastview.download.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import com.huawei.fastengine.fastview.Config;
import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;

public final class PackageUtils {
    private static final int GAMEBOX_SUPPORT_VERSION = 10008001;
    public static final String HWGAMEBOX_PACKAGE_NAME = "com.huawei.gamebox";
    public static final String TAG = "PackageUtils";
    private static final int URL_SUPPORT_VERSION = 10002001;

    public static boolean isEngineInstalled(Context context) {
        PackageManager packageManager = context.getPackageManager();
        if (packageManager != null) {
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(Config.FASTAPP_PACKAGE_NAME, 64);
                if (packageInfo.versionCode >= URL_SUPPORT_VERSION) {
                    FastViewLogUtils.w(TAG, "Fastapp Engine is installed!");
                    return true;
                }
                FastViewLogUtils.w(TAG, "Fastapp Engine's versionCode isn't supported! " + packageInfo.versionCode);
            } catch (NameNotFoundException e) {
                FastViewLogUtils.w(TAG, "Fastapp Engine is not installed!");
            }
        }
        return false;
    }

    public static boolean isEngineInstalledIngoreVersion(Context context) {
        PackageManager packageManager = context.getPackageManager();
        if (packageManager != null) {
            try {
                if (packageManager.getPackageInfo(Config.FASTAPP_PACKAGE_NAME, 64).versionCode > 0) {
                    FastViewLogUtils.i(TAG, "Fastapp Engine is installed ");
                    return true;
                }
                FastViewLogUtils.e(TAG, "Fastapp Engine versionCode is incorrect!");
            } catch (NameNotFoundException e) {
                FastViewLogUtils.e(TAG, "Fastapp Engine is not installed!");
            }
        }
        return false;
    }

    public static boolean isEngineInstalledforGameBox(Context context) {
        PackageManager packageManager = context.getPackageManager();
        if (packageManager != null) {
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(Config.FASTAPP_PACKAGE_NAME, 64);
                if (packageInfo.versionCode >= GAMEBOX_SUPPORT_VERSION) {
                    FastViewLogUtils.w(TAG, "Fastapp Engine is installed!");
                    return true;
                }
                FastViewLogUtils.w(TAG, "Fastapp Engine's versionCode isn't supported! " + packageInfo.versionCode);
            } catch (NameNotFoundException e) {
                FastViewLogUtils.w(TAG, "Fastapp Engine is not installed!");
            }
        }
        return false;
    }

    public static boolean isInstallByPackage(Context context, String str) {
        String str2 = context.getApplicationInfo().packageName;
        if (HWGAMEBOX_PACKAGE_NAME.equals(str2)) {
            return isEngineInstalledforGameBox(context);
        }
        FastViewLogUtils.i(TAG, "Package callPacksgename " + str2);
        try {
            context.getPackageManager().getPackageInfo(str, 0);
            FastViewLogUtils.i(TAG, "Package is installed : " + str);
            return true;
        } catch (NameNotFoundException e) {
            FastViewLogUtils.e(TAG, "isInstallByPackage NameNotFoundException:");
            FastViewLogUtils.i(TAG, "Package is not installed : " + str);
            return false;
        } catch (UnsupportedOperationException e2) {
            FastViewLogUtils.e(TAG, "isInstallByPackage UnsupportedOperationException:");
            FastViewLogUtils.i(TAG, "Package is not installed : " + str);
            return false;
        } catch (Exception e3) {
            FastViewLogUtils.e(TAG, "isInstallByPackage Exception:");
            FastViewLogUtils.i(TAG, "Package is not installed : " + str);
            return false;
        }
    }
}
